package com.xiaoman.jvm.classpath.impl;

import com.xiaoman.jvm.classpath.Entry;

import java.io.IOException;
import java.nio.file.*;

/**
 * 表示ZIP或JAR文件形式的类路径
 */
public class ZipEntry implements Entry {
    private Path absPath;

    public ZipEntry(String path) {
        this.absPath = Paths.get(path).toAbsolutePath();
    }

    @Override
    public byte[] readClass(String className) throws IOException {
        try (FileSystem fileSystem = FileSystems.newFileSystem(absPath, null)) {
            return Files.readAllBytes(fileSystem.getPath(className));
        }
    }

    @Override
    public String String() {
        return absPath.toString();
    }
}
